/**
 *
 */
package com.ssyt.examinationsystem.dao.impl;

import com.jdbc.util.JdbcUtils;
import com.ssyt.examinationsystem.dao.IStudentInfoDAO;
import com.ssyt.examinationsystem.entity.StudentInfo;
import com.ssyt.examinationsystem.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * 数据层的实现类
 *
 * @author Administrator
 */
public class StudentInfoDAOImpl implements IStudentInfoDAO {

    @Override
    public boolean login(StudentInfo stu) {
        String sql = "select 1 from studentinfo where Account=? and password=? and roleId=?";
        Object[] parameters = {stu.getAccount(), stu.getPassword(), stu.getRole().getRoleId()};
        List<Map<String, Object>> list = JdbcUtils.executeQuery(sql, parameters);
        return CollectionUtils.isEmptyForList(list);
    }

    /**
     * 查询所有学生信息
     */
    public List<Map<String, Object>> getStudentInfos() {
        String sql = "select * from studentinfo ";
        return JdbcUtils.executeQuery(sql);
    }

    /**
     * 根据学生编号删除学生信息
     */
    public int deleteStudentInfo(String stuId) {
        String sql = "delete from studentinfo where stuId=?";
        return JdbcUtils.executeUpdate(sql, stuId);
    }

    /**
     * 添加所有学生信息
     */
    public int addStudentInfo(StudentInfo stu) {
        String sql = "insert into studentinfo values(null,?,?,?,?,?)";
        Object[] parameters = {stu.getAccount(), stu.getPassword(), stu.getStuClass(), stu.getRole().getRoleId(),
                stu.getStuName()};
        return JdbcUtils.executeUpdate(sql, parameters);
    }

    /**
     * 修改所有学生信息
     */
    public int updateStudentInfo(StudentInfo stu) {
        String sql = "update studentinfo set account=?,password=?,stuClass=?,roleid=?,stuname=? where stuid=?";
        Object[] parameters = {stu.getAccount(), stu.getPassword(), stu.getStuClass(), stu.getRole().getRoleId(),
                stu.getStuName(), stu.getStuId()};
        return JdbcUtils.executeUpdate(sql, parameters);
    }

    @Override
    public Map<String, Object> getStuByStuId(String stuId) {
        String sql = "select * from studentinfo where stuid=?";
        List<Map<String, Object>> list = JdbcUtils.executeQuery(sql, stuId);
        return CollectionUtils.isEmptyForList(list) ? list.get(0) : null;
    }

    @Override
    public Map<String, Object> getStuByAcc(String account, String password) {
        String sql = "select * from studentinfo where account=? and password=?";
        List<Map<String, Object>> list = JdbcUtils.executeQuery(sql, account, password);
        return CollectionUtils.isEmptyForList(list) ? list.get(0) : null;
    }

}
